/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tools;

import com.openbrew.openbrewrf.BarrelConditioning;
import com.openbrew.openbrewrf.Brews;
import com.openbrew.openbrewrf.Movement;
import com.openbrew.openbrewrf.ScanTblKeg;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import service.enumerations.MovementState;

/**
 *
 * @author chapmand
 */
public class MovementTools {
    
    /**
     * Insert a movement based on kegs being scanned.
     * @param en
     * @param totalv
     * @param kegs
     */
    public void insertKegMovement(final EntityManager en, final double totalv
            , final List<ScanTblKeg> kegs){
        final java.util.Date moveDate = new java.util.Date();
        Movement mvmt = new Movement();
        mvmt.setMovementDate(moveDate);
        mvmt.setMovementBrewID(kegs.get(0).getKegBatch());
        mvmt.setMovementOldState(MovementState.CONDITIONING.getState());
        mvmt.setMovementStateID(MovementState.KEGGED.getState());
        mvmt.setMovementVolume((float)totalv);
        mvmt.setMovementEmpID(1);
        mvmt.setMovementNotes("Package movement inserted by openBrewRF keg scan updates");
        mvmt.setMovementOVID(getSelectedVesselID(en, kegs.get(0).getKegBatch()));
        mvmt.setMovementNVID(0);
        en.persist(mvmt);
    }
    
    /**
     * Insert a movement based on kegs being scanned.
     * @param en
     * @param totalv
     * @param batchID
     * @param employeeID
     * @param moveDate
     */
    public void insertKegMovement(final EntityManager en, final double totalv
            , final int batchID, final int employeeID, final Date moveDate){
        Movement mvmt = new Movement();
        mvmt.setMovementDate(moveDate);
        mvmt.setMovementBrewID(batchID);
        mvmt.setMovementOldState(MovementState.CONDITIONING.getState());
        mvmt.setMovementStateID(MovementState.KEGGED.getState());
        mvmt.setMovementVolume((float)totalv);
        mvmt.setMovementEmpID(employeeID);
        mvmt.setMovementNotes("Package movement inserted by openBrewRF MovementTools class");
        mvmt.setMovementOVID(getSelectedVesselID(en, batchID));
        mvmt.setMovementNVID(0);
        en.persist(mvmt);
    }
    
    /**
     * Insert a movement based on bottles/cans.
     * @param en - Entity Manager
     * @param totalv - total volume moved.
     * @param batchID - ID of batch moved.
     * @param employeeID - ID of employee who moved batch.
     * @param moveDate
     */
    public void insertBottleMovement(final EntityManager en, final double totalv
            , final int batchID, final int employeeID, final Date moveDate){
        Movement mvmt = new Movement();
        mvmt.setMovementDate(moveDate);
        mvmt.setMovementBrewID(batchID);
        mvmt.setMovementOldState(MovementState.CONDITIONING.getState());
        mvmt.setMovementStateID(MovementState.BOTTLED.getState());
        mvmt.setMovementVolume((float)totalv);
        mvmt.setMovementEmpID(employeeID);
        mvmt.setMovementNotes("Package movement inserted by openBrewRF MovementTools class");
        mvmt.setMovementOVID(getSelectedVesselID(en, batchID));
        mvmt.setMovementNVID(0);
        en.persist(mvmt);
    }
    
    /**
     * Insert a barrel aged movement based on bottles/cans.
     * @param en - Entity Manager
     * @param totalv - total volume moved.
     * @param bcid - ID of barrel conditioning batch moved.
     * @param employeeID - ID of employee who moved batch.
     */
    public void insertBarrelBottleMovement(final EntityManager en, final double totalv
            , final int bcid, final int employeeID){
        BarrelConditioning bc = getBarrelConditioning(en, bcid);
        final java.util.Date moveDate = new java.util.Date();
        Movement mvmt = new Movement();
        mvmt.setMovementDate(moveDate);
        mvmt.setMovementBrewID(determineBatchID(bc));
        mvmt.setMovementOldState(MovementState.BARREL_FOR_PACKAGING.getState());
        mvmt.setMovementStateID(MovementState.BOTTLED.getState());
        mvmt.setMovementVolume((float)totalv);
        mvmt.setMovementEmpID(employeeID);
        mvmt.setMovementNotes("Barrel Aged package movement inserted by openBrewRF MovementTools class");
        mvmt.setMovementOVID(bc.getBCVesselID());
        mvmt.setMovementNVID(0);
        en.persist(mvmt);
    }
    
    /**
     * Insert a barrel aged movement based on kegs.
     * @param en - Entity Manager
     * @param totalv - total volume moved.
     * @param bcid - ID of barrel conditioning batch moved.
     * @param employeeID - ID of employee who moved batch.
     */
    public void insertBarrelKegMovement(final EntityManager en, final double totalv
            , final int bcid, final int employeeID){
        BarrelConditioning bc = getBarrelConditioning(en, bcid);
        final java.util.Date moveDate = new java.util.Date();
        Movement mvmt = new Movement();
        mvmt.setMovementDate(moveDate);
        mvmt.setMovementBrewID(determineBatchID(bc));
        mvmt.setMovementOldState(MovementState.BARREL_FOR_PACKAGING.getState());
        mvmt.setMovementStateID(MovementState.KEGGED.getState());
        mvmt.setMovementVolume((float)totalv);
        mvmt.setMovementEmpID(employeeID);
        mvmt.setMovementNotes("Barrel Aged package movement inserted by openBrewRF MovementTools class");
        mvmt.setMovementOVID(bc.getBCVesselID());
        mvmt.setMovementNVID(0);
        en.persist(mvmt);
    }
    
    private BarrelConditioning getBarrelConditioning(final EntityManager en, final int bcsid){
        TypedQuery<BarrelConditioning> query = en.createQuery(
                "SELECT b FROM BarrelConditioning b WHERE b.bCId = ?1", BarrelConditioning.class);
        query.setParameter(1, bcsid);
        BarrelConditioning bc = query.getSingleResult();
        return bc;
    }
    
    private int determineBatchID(final BarrelConditioning bc){
        final String[] sarry = bc.getBCBatches().split(",");
        String bOne = "";
        boolean same = true;
        if(sarry.length > 1){
            bOne = sarry[0];
            for(String batchID : sarry){
                if (!bOne.equals(batchID)){
                    same = false;
                }
            }
        } else {
            bOne = bc.getBCBatches();
        }
        if (same){
            return Integer.parseInt(bOne);
        } else {
            return 0;
        }
    }
    
    private int getSelectedVesselID(final EntityManager en, final int batchID){
        TypedQuery<Brews> query = en.createQuery(
                "SELECT b FROM Brews b WHERE b.bid = ?1", Brews.class);
        query.setParameter(1, batchID);
        Brews brew = query.getSingleResult();
        return brew.getCvid();
    }
    
    /**
     * Update brews based on movements inserted.
     * @param en - Entity Manager
     * @param movement - movement being inserted.
     * @param temperature
     * @param gravity
     * @param time
     * @param notes
     * @param volume
     * @param psi
     */
    public void updateBrewBasedOnMovement(final EntityManager en, final Movement movement
            , final float temperature, final float gravity, final java.sql.Time time
            , final String notes, final float volume, final float psi){
        if(movement.getMovementOldState() == MovementState.FERMENTING.getState()
                && movement.getMovementStateID() == MovementState.LAGERING.getState()){
            //update brew for lagering.
            final Brews brew = getBrewFromID(movement.getMovementBrewID(), en);
            brew.setBState(movement.getMovementStateID());
            brew.setVolLeft(volume);
            brew.setSFTemp01(temperature);
            brew.setSFGrav01(gravity);
            brew.setSFDate01(movement.getMovementDate());
            brew.setSFTime01(time);
            brew.setSFNote01(notes);
            brew.setSFVol01(volume);
            brew.setSfvid(movement.getMovementNVID());
            en.merge(brew);
        } else if (movement.getMovementOldState() == MovementState.LAGERING.getState()
                && movement.getMovementStateID() == MovementState.CONDITIONING.getState()){
            //update brew for conditioning.
            final Brews brew = getBrewFromID(movement.getMovementBrewID(), en);
            brew.setBState(movement.getMovementStateID());
            brew.setBtg(gravity);
            brew.setCTDate(movement.getMovementDate());
            brew.setCTTime(time);
            brew.setBpsi(psi);
            brew.setCTTemp(temperature);
            brew.setCTVol(volume);
            brew.setCvid(movement.getMovementNVID());
            brew.setVolLeft(volume);
            en.merge(brew);
        }
    }
    
    public Brews getBrewFromID(final int brewID, final EntityManager en){
        TypedQuery<Brews> query = en.createQuery(
                    "SELECT b FROM Brews b WHERE b.bid = ?1", Brews.class);
        query.setParameter(1, brewID);
        final Brews brew = query.getSingleResult();
        return brew;
    }
}
